package view;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class LoginView extends Application{
    public static void main(String[] args) {
        Application.launch(args);
    }

    private String adminName = "admin";
    private String adminPassword = "admin";



    public String getAdminName() {
        return adminName;
    }
    public String getAdminPassword() {
        return adminPassword;
    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("模拟OS");
        primaryStage.initStyle(StageStyle.DECORATED);

        TextField adminNameField = new TextField("请输入管理员账号");
        PasswordField adminPasswordFiled = new PasswordField();
        Label label = new Label("欢迎使用模拟OS");
        Label adminNameLabel = new Label("账号:");
        Label adminPasswordLabel = new Label("密码:");
        Button loginButton = new Button("登录");
        HBox labelBox = new HBox();
        HBox adminNameBox = new HBox();
        HBox adminPasswordBox = new HBox();
        HBox loginButtonBox = new HBox();
        VBox mainBox = new VBox();

        labelBox.getChildren().add(label);
        labelBox.setAlignment(Pos.CENTER);
        labelBox.setSpacing(20);
        adminNameBox.getChildren().addAll(adminNameLabel, adminNameField);
        adminNameBox.setAlignment(Pos.CENTER);
        adminNameBox.setSpacing(20);
        adminPasswordBox.getChildren().addAll(adminPasswordLabel, adminPasswordFiled);
        adminPasswordBox.setAlignment(Pos.CENTER);
        adminPasswordBox.setSpacing(20);
        loginButtonBox.getChildren().add(loginButton);
        loginButtonBox.setAlignment(Pos.CENTER);
        loginButtonBox.setSpacing(20);
        loginButton.setAlignment(Pos.BASELINE_RIGHT);

        mainBox.setAlignment(Pos.CENTER);
        mainBox.setSpacing(20);
        mainBox.getChildren().addAll(labelBox,adminNameBox,adminPasswordBox,loginButtonBox);

        primaryStage.setScene(new Scene(mainBox,400,250));
        primaryStage.show();

        loginButton.setOnAction(event -> {
            String enteredAdminName = adminNameField.getText();
            System.out.println(enteredAdminName);
            String enteredAdminPassword = adminPasswordFiled.getText();
            System.out.println(enteredAdminPassword);
            if(enteredAdminName.equals(getAdminName()) && enteredAdminPassword.equals(getAdminPassword())) {
                primaryStage.hide();
                new MainView(primaryStage);
            }
            else {
                Stage stage = new Stage();
                Text text = new Text("账号或密码错误");
                Button button = new Button("确定");
                VBox vBox = new VBox();
                HBox hBox = new HBox();
                hBox.getChildren().add(button);
                hBox.setAlignment(Pos.CENTER);
                vBox.getChildren().addAll(text,hBox);
                stage.setScene(new Scene(vBox,100,200));

                stage.setHeight(100);
                stage.setWidth(200);
                stage.initOwner(primaryStage);//设置父窗口
                stage.initModality(Modality.APPLICATION_MODAL);//窗口打开时，其他窗口无法操作
                stage.show();

                button.setOnAction(event1 -> {
                    stage.close();
                });
            }
        });

    }
}
